// pages/compass/compass.js
let lastDirection = 0;
let times = 0;
Page({
  data: {
    direction: 0,
    textDirection: '',
    durationDes: '',
    animationData: {},
    longitudeDes: '',
    latitudeDes: '',
    state: 2,      // 1 加载中 2 请将手机沿如图所示方向摇晃数次，以校准指南针 3 加载完成
  },

  onLoad: function (options) {
    require("../../utils/util.js").setTitle();
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.init();
  },
  init: function () {
    wx.showLoading({ title: '加载中' });
    let self = this;
    wx.startCompass({
      success: function () {// sucess callback
        self.setData({ state: 3 });
        self.compass();
        self.location();
        self.draw();
      },
      fail: function () {//fail callback
        self.setData({ state: 2 });
      },
      complete: function () {//complete callback
        wx.hideLoading();
      }
    });
  },
  onReady: function () {
  },
  // 获取方向
  compass: function () {
    wx.startCompass();
    let self = this;
    console.log('22')
    self.setData({
      direction: 90,
      durationDes: Math.round(90) + '°',
      textDirection: require('../../config/direction.js')[Math.ceil(Math.round(90) / 15)]
    });

    wx.onCompassChange(function (res) {
      let diff = lastDirection - res.direction;
      if (diff > 1 || diff < -1) {
        let curDrection = res.direction;
        self.setData({
          direction: res.direction,
          durationDes: Math.round(res.direction) + '°',
          textDirection: require('../../config/direction.js')[Math.ceil(Math.round(res.direction) / 15)]
        });
        if (lastDirection < 180 && (curDrection - lastDirection > 180)) { // 4 到 1
          times++;
        }
        if (lastDirection > 180 && curDrection - lastDirection < -180) { // 1 到 4
          times--;
        }
        curDrection = curDrection - times * 360;
        self.doAnimation(curDrection);
        lastDirection = res.direction;
      }
    });
  },

  draw: function () {
    require('../../utils/arc.js').arc("arcRed");
  },

  // 获取经纬度
  location: function () {
    let self = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let latitude = res.latitude;
        let longitude = res.longitude;
        let speed = res.speed;
        let accuracy = res.accuracy;
        let latitudeDes = self.formatLocation(latitude);
        let longitudeDes = self.formatLocation(longitude);
        self.setData({
          longitudeDes: (longitude > 0 ? '东经：' : '西经：') + longitudeDes,
          latitudeDes: (latitude > 0 ? '北纬：' : '南纬：') + latitudeDes,
        })
      }
    })
  },

  formatLocation: function (degrees) {
    let hour = parseInt(degrees);
    let tmpminute = (degrees - hour) * 60;
    let minute = parseInt(tmpminute);
    let second = parseInt((tmpminute - minute) * 60);
    if (!hour && !minute && !second) {
      return '';
    }
    if (minute && !second) {
      return hour + '°' + minute + "'";
    }
    if (hour && !minute && !second) {
      return hour + '°';
    }
    return hour + '°' + minute + "'" + second + '"';
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  doAnimation: function (degreres, duration) {
    var animation = wx.createAnimation({
      duration: (duration || duration == 0) ? duration : 300,
      timingFunction: 'ease',
    })
    this.animation = animation
    animation.rotate(-degreres).step()
    this.setData({
      animationData: animation.export()
    })
  },
  iknow: function () {
    this.setData({ state: 3 });
  }
})